{
 "cells": [
  {
   "cell_type": "raw",
   "id": "8980c8b0",
   "metadata": {},
   "source": [
    "---\n",
    "sidebar_position: 7\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c3f1df8",
   "metadata": {},
   "source": [
    "# Self-ask with search\n",
    "\n",
    "This walkthrough showcases the self-ask with search agent."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2018da2d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain import hub\n",
    "from langchain.agents import AgentExecutor, create_self_ask_with_search_agent\n",
    "from langchain_community.llms import Fireworks\n",
    "from langchain_community.tools.tavily_search import TavilyAnswer"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "527080a7",
   "metadata": {},
   "source": [
    "## Initialize Tools\n",
    "\n",
    "We will initialize the tools we want to use. This is a good tool because it gives us **answers** (not documents)\n",
    "\n",
    "For this agent, only one tool can be used and it needs to be named \"Intermediate Answer\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "655bcacd",
   "metadata": {},
   "outputs": [],
   "source": [
    "tools = [TavilyAnswer(max_results=1, name=\"Intermediate Answer\")]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cec881b8",
   "metadata": {},
   "source": [
    "## Create Agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9860f2e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get the prompt to use - you can modify this!\n",
    "prompt = hub.pull(\"hwchase17/self-ask-with-search\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0ac6b463",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Choose the LLM that will drive the agent\n",
    "llm = Fireworks()\n",
    "\n",
    "# Construct the Self Ask With Search Agent\n",
    "agent = create_self_ask_with_search_agent(llm, tools, prompt)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2e90540",
   "metadata": {},
   "source": [
    "## Run Agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6677fa7f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create an agent executor by passing in the agent and tools\n",
    "agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "fff795f0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m Yes.\n",
      "Follow up: Who is the reigning men's U.S. Open champion?\u001b[0m\u001b[36;1m\u001b[1;3mThe reigning men's U.S. Open champion is Novak Djokovic. He won his 24th Grand Slam singles title by defeating Daniil Medvedev in the final of the 2023 U.S. Open.\u001b[0m\u001b[32;1m\u001b[1;3m\n",
      "So the final answer is: Novak Djokovic.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'input': \"What is the hometown of the reigning men's U.S. Open champion?\",\n",
       " 'output': 'Novak Djokovic.'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent_executor.invoke(\n",
    "    {\"input\": \"What is the hometown of the reigning men's U.S. Open champion?\"}\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "635a97a2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.1"
  },
  "vscode": {
   "interpreter": {
    "hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
